﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using WEB.BLL.Entities;
using WEB.BLL.Services;
using WEB.BLL;
using System.Web;
using Ext.Net;
using WEB.Core.Controls;
using WEB.Core.Util;

namespace WEB.APP.Controls.System
{
    public partial class RoleForm : BaseControl
    {
        public event EventHandler SaveClick;
        private void OnSaveClick()
        {
            if (SaveClick != null)
            {
                SaveClick(this, EventArgs.Empty);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public void Show()
        {
            FormPanel1.Reset();
            winDetails.Show();
            txtRoleName.Focus(true, 500);
        }
        public void Show(int roleId, bool isEdit)
        {
            RoleEntity roleE = new RoleEntity();
            RoleService roleS = RoleService.GetInstance();
            roleE.RoleId = roleId;
            roleE = roleS.LoadByPrimaryKey(roleE);
            if (null == roleE)
                return;
            hdfRoleId.Value = roleE.RoleId;
            txtRoleName.Text = roleE.RoleName;
            txtDescription.Text = roleE.Description;
            ckbIsActive.Checked = roleE.IsActive;
            btnSave.Hidden = !isEdit;
            winDetails.Show();
            txtRoleName.Focus(true, 500);
        }
        protected void btnSave_Click(object sender, DirectEventArgs e)
        {
            try
            {
                RoleEntity roleE = new RoleEntity();
                RoleService roleS = RoleService.GetInstance();
                roleE.RoleName = txtRoleName.Text;
                roleE.Description = txtDescription.Text;
                roleE.IsActive = ckbIsActive.Checked;
                roleE.UpdatedDate = DateTime.Now;
                roleE.CreatedDate = DateTime.Now;
                roleE.CreatedBy = roleE.UpdatedBy = WEBContext.Current.User.UserName;
                if (!string.IsNullOrEmpty(hdfRoleId.Text))
                {
                    roleE.RoleId = Format.ConvertToInt(hdfRoleId.Text);
                    roleS.Update(roleE);
                }
                else
                {
                    roleS.Insert(roleE);
                }
                this.winDetails.Hide();
                OnSaveClick();
            }
            catch (Exception ex)
            {
                Notification(ex);
            }
        }
    }
}